<?php
// +----------------------------------------------------------------------
// | Created by Sublime: [ Conqweal ]
// +----------------------------------------------------------------------
// | Copyright (c) 2021~20223[chenjia] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.99dn.cn
// +----------------------------------------------------------------------
// | Author: Conqweal <150672834@qq.com>
// +----------------------------------------------------------------------
// | Date: 2021/12/22 0026
// +----------------------------------------------------------------------
// | Description:验证接口
// +----------------------------------------------------------------------

namespace app\api\controller;

use app\api\controller\Api;
use app\api\model\User;
use think\facade\Db;
use think\facade\Cache;
use app\ApiController;
use support\Request;
class ValidateController extends ApiController
{
    protected $noNeedLogin = '*';
    protected $layout = '';
    protected $error = null;

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 检测邮箱
     *
     * @ApiMethod (POST)
     * @param string $email 邮箱
     * @param string $u_id    排除会员ID
     */
    public function check_email_available()
    {
        $email = $this->request->post('email');
        $u_id = (int)$this->request->post('u_id');
        $count = User::where('email', '=', $email)->where('u_id', '<>', $u_id)->count();
        if ($count > 0) {
            return $this->error(__('邮箱已经被占用'));
        }
        return $this->success();
    }

    /**
     * 检测用户名
     *
     * @ApiMethod (POST)
     * @param string $username 用户名
     * @param string $u_id       排除会员ID
     */
    public function check_username_available()
    {
        $username = $this->request->post('username');
        $u_id = (int)$this->request->post('u_id');
        $count = User::where('username', '=', $username)->where('u_id', '<>', $u_id)->count();
        if ($count > 0) {
            return $this->error(__('用户名已经被占用'));
        }
        return $this->success();
    }

    /**
     * 检测昵称
     *
     * @ApiMethod (POST)
     * @param string $nickname 昵称
     * @param string $u_id       排除会员ID
     */
    public function check_nickname_available()
    {
        $nickname = $this->request->post('nickname');
        $u_id = (int)$this->request->post('u_id');
        $count = User::where('nickname', '=', $nickname)->where('u_id', '<>', $u_id)->count();
        if ($count > 0) {
            return $this->error(__('昵称已经被占用'));
        }
        return $this->success();
    }

    

   

    /**
     * 检测邮箱
     *
     * @ApiMethod (POST)
     * @param string $mobile 邮箱
     */
    public function check_email_exist()
    {
        $email = $this->request->post('email');
        $count = User::where('email', '=', $email)->count();
        if (!$count) {
            return $this->error(__('邮箱不存在'));
        }
        return $this->success();
    }

   

    /**
     * 检测邮箱验证码
     *
     * @ApiMethod (POST)
     * @param string $email   邮箱
     * @param string $captcha 验证码
     * @param string $event   事件
     */
    public function check_ems_correct()
    {
        $email = $this->request->post('email');
        $captcha = $this->request->post('captcha');
        $event = $this->request->post('event');
        if (!\app\api\library\Ems::check($email, $captcha, $event)) {
            return $this->error(__('验证码不正确'));
        }
        return $this->success();
    }
}